package cn.seaboot.commons.lang;

import cn.seaboot.commons.core.Asserts;

import java.io.Serializable;
import java.util.Objects;

/**
 * 键值对
 * <p>
 * Basic implementation of {@link NameValuePair}.
 *
 * @author Mr.css
 * @version 2024-03-01 16:37
 */
public class NameValueEntity implements NameValuePair, Serializable {
	private static final long serialVersionUID = -8776373631319637990L;
	private final String name;
    private final String value;

    /**
     * Default Constructor taking a name and a value. The value may be null.
     *
     * @param name  The name.
     * @param value The value.
     */
    public NameValueEntity(final String name, final String value) {
        super();
        Asserts.notNull(name, "Name is null");
        this.name = name;
        this.value = value;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public String getValue() {
        return this.value;
    }

    @Override
    public String toString() {
        // don't call complex default formatting for a simple toString
        if (this.value == null) {
            return name;
        }
        return this.name + "=" + this.value;
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof NameValueEntity) {
            final NameValueEntity that = (NameValueEntity) obj;
            return this.name.equalsIgnoreCase(that.name) && Objects.equals(this.value, that.value);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, value);
    }
}
